var app = getApp();
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		date:'',
		page:1,
		limit:20,
		list: []
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var month=util.getNowMonth();
		this.setData({
			date: month,
		})
		this.getList()
	},

	getList:function(){
		var that = this;
		api.POST({
			url: app.globalData.base_url + 'Mealticket/getCancelList',
			params: {
				page: that.data.page,
				limit: that.data.limit,
				time_str: that.data.date
			},
			success: function (res) {
				wx.stopPullDownRefresh();
				util.hiddenLoading();
				if (res.data.code == '') {
					var currentPage = res.data.data.search.page;
					var total_page = res.data.data.search.total_page;
					if (currentPage == 1) {
						var list = [];
					} else {
						var list = that.data.list;
					}
					list = list.concat(res.data.data.list);
					that.setData({
						total_page: total_page,
						list: list,
						report: res.data.data.report,
						dataHidden: (list.length > 0) ? 1 : 2
					})
				} else {
					util.showError(res.data.message)
				}
			},
			fail: function (fail) {
				wx.stopPullDownRefresh();
				util.hiddenLoading();
				util.showError("网络错误，请检查网络。");
			}
		});
	},

	//选择日期
	bindDateChange:function(e){
		this.setData({
			date: e.detail.value
		})
		this.getList();
	},

	//加载更多
	onReachBottom: function () {
		var page = this.data.page;
		var total_page = this.data.total_page;
		if (page < total_page) {
			this.setData({
				page: page + 1
			})
			this.getList();
		}
	},

	//刷新
	onPullDownRefresh: function () {
		this.refreshData();
	},

	//刷新数据
	refreshData: function () {
		var that = this;
		that.setData({
			page: 1
		})
		setTimeout(function () {
			that.getList();
		}, 500)
	},

})